﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Cartoon.Model;
using System.Windows.Threading;
using System.Windows.Media.Media3D;
using System.ComponentModel;

namespace CartoonViewer
{
    public delegate void CartoonSelectedDelegate(object sender,Cartoon.Model.Comic comic);

    /// <summary>
    /// UC_Cartoon.xaml 的交互逻辑
    /// </summary>
    public partial class UC_Cartoon : UserControl
    {  
        public UC_Cartoon()
        {
            InitializeComponent();

            //3D旋转初始化

            //实例化旋转工作线程
            EHRotate3DTimer = new EventHandler(Rotate3DTimer_Tick);
            Rotate3DBGWorker = new BackgroundWorker();
            this.Rotate3DBGWorker.WorkerReportsProgress = true;
            this.Rotate3DBGWorker.WorkerSupportsCancellation = true; 
            
            Rotate3DBGWorker.DoWork += new DoWorkEventHandler(Rotate3DBGWorker_DoWork);
            Rotate3DBGWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Rotate3DBGWorker_RunWorkerCompleted);
            Rotate3DBGWorker.ProgressChanged += new ProgressChangedEventHandler(Rotate3DBGWorker_ProgressChanged);
        }

        

        BackgroundWorker Rotate3DBGWorker;
        public UC_Cartoon(Comic _comic):this()
        {
            this.comic = _comic;
            this.ReLoad();
        }

        protected bool isSelected;
        public bool IsSelected { 
            get { return this.isSelected; } 
            set 
            { 
                this.isSelected = value;
                if (this.isSelected == true)
                {
                    this.Cvs_Comic.Background = Brushes.Red;
                }
                else
                {
                    this.Cvs_Comic.Background = Brushes.Transparent;
                }
            }
        }
        public Comic comic { get; set; }
        public event CartoonSelectedDelegate Selected;
        public void ReLoad()
        {
            SetImageSource(this.comic.PictureUrl);
            this.Lb_Name.Content = this.comic.Name;
        }

        private void SetImageSource(string fileurl)
        {
            BitmapSource imgSource = new BitmapImage(new Uri(fileurl, UriKind.RelativeOrAbsolute));
            this.Panle1Img.ImageSource = imgSource;
            this.Panle2Img.ImageSource = imgSource;
            this.Img_CartoonFront.Source = imgSource;
            this.Img_CartoonBack.Source = imgSource;
        }

        public double W
        {
            get
            {
                return this.Cvs_Comic.Width;
            }
        }

        public double H
        {
            get
            {
                return this.Cvs_Comic.Height;
            }
        }

        protected void OnSelected(Comic c)
        {
            if (this.Selected != null)
            {
                this.Selected(this,c);
            }
        }

        private void Cvs_Comic_MouseEnter(object sender, MouseEventArgs e)
        {
            //this.Cvs_Comic.Background = Brushes.Blue;
            TransImage3D();
           
        }

        private void Cvs_Comic_MouseLeave(object sender, MouseEventArgs e)
        {
            Brush b = Brushes.Transparent;
            if (this.IsSelected == true)
            {
                b = Brushes.Red;
            }
            this.Cvs_Comic.Background = b;

            StopRotate3DTimer();
        }

        private void Cvs_Comic_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.OnSelected(this.comic);
        }


        #region 动画效果

        private DispatcherTimer Rotate3DTimer;
        private bool Rotate3DTurning = false;
        private double SpeedRotate3D = 1;

        private void TransImage3D()
        { 
            if (Rotate3DTurning == true)
            {
                StopRotate3DTimer();
                return;
            }

            StartRotate3DTimer();
            
        }

        private double zSpeed = 1;
        private void Rotate3DTimer_Tick(object sender, EventArgs e)
        {
            this.Tran3DRotate1.Angle = (SpeedRotate3D + this.Tran3DRotate1.Angle) % 360;
            this.Tran3DRotate2.Angle = this.Tran3DRotate1.Angle - 180;
            if (this.Tran3DRotate1.Angle == 180)
            {
                StopRotate3DTimer();
            } 
        }

        private void StopRotate3DTimer()
        {

            if (Rotate3DTimer == null) return;

            Rotate3DTimer.Tick -= EHRotate3DTimer;

            Rotate3DTurning = false;
             
            Rotate3DTimer.Stop();
            this.Viewport3D00.Visibility = Visibility.Hidden;
            this.FrontBorder.Visibility = Visibility.Visible;
            this.BackBorder.Visibility = Visibility.Visible;

            Rotate3DTimer = null;

             
            GC.Collect();

        }

        private EventHandler EHRotate3DTimer;

        private void StartRotate3DTimer()
        {
            if (Rotate3DTurning == true) return;

            //旋转原理：构造一个二面体3D模型，正面和后面分别使用同一张
            //图片。旋转时，前后两张图片的的角度差一直保持180度

            this.Tran3DRotate1.Angle = 0;
            this.Tran3DRotate2.Angle = this.Tran3DRotate1.Angle - 180;

            this.Viewport3D00.Visibility = Visibility.Visible;
            this.FrontBorder.Visibility = Visibility.Hidden;
            this.BackBorder.Visibility = Visibility.Hidden;

            this.Rotate3DBGWorker.RunWorkerAsync();
            
        }

        private void Rotate3DBGWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Rotate3DTurning = true;

            //实例化3D旋转计时器
            Rotate3DTimer = new DispatcherTimer();
            Rotate3DTimer.Tick += EHRotate3DTimer;

            Rotate3DTurning = true;

            Rotate3DTimer.Interval = new TimeSpan(3000);
            Rotate3DTimer.Start(); 
        }

        private void Rotate3DBGWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true)
            { 
                
            } 
            
        }

        private void Rotate3DBGWorker_DoWork(object sender, DoWorkEventArgs e)
        {

            BackgroundWorker w = sender as BackgroundWorker;
            w.ReportProgress(100);
        }

        #endregion

    }
}
